<?php include("_header.phtml") ?>
<?php $field_names = [
    'title' => 'Title',
    'description' => 'Description',
    'category_id' => 'Category',
    'cost' => 'License',
    'download_provider' => 'Repository Provider',
    'browse_url' => 'Repository Url',
    'issues_url' => 'Issues Url',
    'godot_version' => 'Godot version',
    'version_string' => 'Version String',
    'download_commit' => 'Download Commit',
    'download_url' => 'Download Url (Computed)',
    'icon_url' => 'Icon Url',
];
$preview_field_names = [
    'type' => 'Type',
    'link' => 'Image/Video URL',
    'thumbnail' => 'Thumbnail',
]; ?>

<?php function output_field($data, $name, $categories = []) { ?>
    <?php if (strpos($name, 'url') !== false || strpos($name, 'link') !== false || strpos($name, 'thumbnail') !== false) { ?>
        <a href="<?php echo esc($data) ?>" rel="nofollow"><?php echo esc($data) ?></a>
        <?php if ($name == 'icon_url') { ?>
            <br><img src="<?php echo esc($data) ?>" width="80" height="80"></a>
        <?php }?>
        <?php if ($name == 'preview_link') { ?>
            <br><img src="<?php echo esc($data) ?>" style="max-height:80px"></a>
        <?php }?>
        <?php if ($name == 'preview_thumbnail') { ?>
            <br><img src="<?php echo esc($data) ?>" style="max-height:345px"></a>
        <?php }?>
    <?php } elseif($name == 'godot_version') { ?>
        Godot <?php echo raw(ucfirst(str_replace('_', ' ', $data))) ?>
    <?php } elseif($name == 'category_id') { ?>
        <?php echo raw($categories[$data]['name']) ?>
    <?php } elseif($name == 'description') { ?>
        <?php echo nl2br(esc($data)) ?>
    <?php } else { ?>
        <?php echo esc($data) ?>
    <?php } ?>
<?php } ?>

<h1>
    <?php if(isset($data['original'])) { ?>
        Edit of asset
        <a href="<?php echo raw($basepath) ?>/asset/<?php echo raw($data['asset_id']) ?>">"<?php echo esc($data['original']['title']) ?>"</a>
    <?php } else { ?>
        Creation of asset
        <?php if($data['asset_id'] != -1) { ?>
            <a href="<?php echo raw($basepath) ?>/asset/<?php echo url($data['asset_id']) ?>">"<?php echo esc($data['title']) ?>"</a>
        <?php } else { ?>
            <a href="#">"<?php echo esc($data['title']) ?>"</a>
        <?php } ?>
    <?php } ?>
    <span class="label label-<?php echo raw(['new' => 'info', 'in_review' => 'primary', 'rejected' => 'danger', 'accepted' => 'success'][$data['status']]) ?>">
        <?php echo raw(ucfirst(str_replace('_', ' ', $data['status']))) ?>
    </span>
</h1>

<?php if(isset($user) && $data['user_id'] == $user['user_id'] && $data['status'] == 'new') { ?>
    <div class="form-group">
        <a class="btn btn-success" href="<?php echo raw($basepath) ?>/asset/edit/<?php echo url($data['edit_id']) ?>/edit">Edit modifications</a>
    </div>
<?php } ?>

<?php if(isset($data['warning'])) { ?>
    <div class="alert alert-warning" role="alert">
        <?php foreach ($data['warning'] as $warning) { ?>
            <strong>Warning:</strong> <?php echo esc($warning) ?> <br><br>
        <?php } ?>
        <em>If you think that this warning is incorrect, you can always <a href="https://github.com/godotengine/asset-library/issues">open an issue</a>.</em>
    </div>
<?php } ?>

<?php if(isset($user) && $data['user_id'] == $user['user_id'] && $data['status'] == 'new') { ?>
    <div class="alert alert-info" role="alert">
        <?php if($data['asset_id'] != -1) { ?>
            This edit you made is currently pending approval by our moderators. <strong>When it is approved, changes would finally go live</strong>. To access this page again later, you can use the "View pending" button on the asset's page, or just use the "Feed" button in the toolbar. <!--You will be notified by email when decision is made.-->
        <?php } else { ?>
            This new asset is currently pending approval by our moderators. <strong>Only when it is approved, it would be searchable</strong>. To access this page again later, you can use the <em>"View pending"</em> button on the "My assets" page, or just use the "Feed" button in the toolbar. <!--You will be notified by email when decision is made.-->
        <?php } ?>
    </div>
<?php } ?>

<?php if($data['status'] == 'rejected') { ?>
    <div class="alert alert-danger" role="alert">
        This asset <?php if($data['asset_id'] != -1) echo 'edit ' ?>has been rejected. Reason: <?php echo esc($data['reason']) ?>
    </div>
<?php } ?>

<table class="table table-bordered table-striped table-edit">
    <thead>
        <tr>
            <th></th>
            <?php if(isset($data['original'])) { ?>
                <th style="min-width: 50%" class="text-center">Old/Current</th>
            <?php } ?>
            <th style="min-width: 50%" class="text-center">New/Edit</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($field_names as $field => $name) { ?>
            <tr>
                <th scope="row">
                    <?php echo raw($name) ?>
                </th>
                <?php if(isset($data['original']) && $data[$field] === null) { ?>

                    <td colspan="2" class="text-center">
                        <?php output_field($data['original'][$field], $field, $categories) ?>
                    </td>

                <?php } else { ?>
                    <?php if(isset($data['original'])) { ?>

                        <td class="text-right danger">
                            <?php output_field($data['original'][$field], $field, $categories) ?>
                        </td>

                    <?php } ?>

                    <td class="text-left success">
                        <?php output_field($data[$field], $field, $categories) ?>
                    </td>

                <?php } ?>
            </tr>
        <?php } ?>
    </tbody>
</table>
<?php foreach($data['previews'] as $i => $preview) if(isset($preview['edit_preview_id'])) { ?>
    <table class="table table-bordered table-striped table-edit">
        <thead>
            <tr>
                <th></th>
                <th colspan="2" class="text-center">
                    Preview
                    <span class="label label-danger"><?php output_field(ucfirst($preview['operation']), 'preview_operation') ?></span>
                </th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($preview_field_names as $field => $name) { ?>
                <tr>
                    <th scope="row">
                        <?php echo raw($name) ?>
                    </th>
                    <?php if(isset($preview['original']) && $preview['operation'] != "remove" && ($preview[$field] === null || $preview[$field] === $preview['original'][$field])) { ?>

                        <td colspan="2" class="text-center active">
                            <?php output_field($preview['original'][$field], 'preview_' . $field, $categories) ?>
                        </td>

                    <?php } else { ?>
                        <?php if(isset($preview['original'])) { ?>

                            <?php if($preview['operation'] == "remove") { ?>
                                <td class="text-center danger" colspan="2">
                                    <?php output_field($preview['original'][$field], 'preview_' . $field, $categories) ?>
                                </td>
                            <?php } else { ?>
                                <td class="text-right danger">
                                    <?php output_field($preview['original'][$field], 'preview_' . $field, $categories) ?>
                                </td>
                                <td class="text-left success">
                                    <?php output_field($preview[$field], 'preview_' . $field, $categories) ?>
                                </td>
                            <?php } ?>

                        <?php } else { ?>
                            <td class="text-center success" colspan="2">
                                <?php output_field($preview[$field], 'preview_' . $field, $categories) ?>
                            </td>
                        <?php } ?>

                    <?php } ?>
                </tr>
            <?php } ?>
        </tbody>
    </table>
<?php } ?>
<?php if(isset($user)) { ?>
    <?php if($user['type'] >= $constants['user_type']['moderator']) { ?>
        <?php if($data['status'] == 'new') { ?>
            <form class="form-inline" action="<?php echo raw($basepath) ?>/asset/edit/<?php echo url($data['edit_id']) ?>/review" method="post">
                <?php include("_csrf.phtml") ?>
                <button type="submit" class="btn btn-primary">Put in queue</button>
            </form>
        <?php } elseif($data['status'] == 'in_review') { ?>

            <form class="form-horizontal" action="<?php echo raw($basepath) ?>/asset/edit/<?php echo url($data['edit_id']) ?>/accept" method="post">
                <?php include("_csrf.phtml") ?>

                <button type="submit" class="btn btn-success col-md-1">Accept</button>

                <div class="form-group col-md-11">
                    <div class="input-group col-md-12" style="min-height: 34px">

                        <?php if($data['asset_id'] == -1) { ?>
                            <label class="control-label col-md-2" for="support_level">Support level:</label>

                            <div class="col-md-10">
                                <select id="support_level" name="support_level" class="form-control btn btn-default">
                                    <?php foreach($constants['support_level'] as $id => $name) if(is_int($id)) { ?>
                                        <option value="<?php echo esc($name) ?>" <?php if($name == 'community') echo 'selected=""'; ?>>
                                            <?php echo esc(ucfirst($name)) ?>
                                        </option>
                                    <?php } ?>
                                </select>
                            </div>
                        <?php } ?>
                    </div>
                </div>

            </form>

            <form class="form-horizontal" action="<?php echo raw($basepath) ?>/asset/edit/<?php echo url($data['edit_id']) ?>/reject" method="post">
                <?php include("_csrf.phtml") ?>

                <button type="submit" class="btn btn-danger col-md-1">Reject</button>

                <div class="form-group col-md-11">
                    <div class="input-group col-md-12">
                        <label class="control-label col-md-2" for="reason" required>Reason:</label>

                        <div class="col-md-10">
                            <textarea class="form-control" id="reason" name="reason" rows="4"></textarea>
                        </div>
                    </div>
                </div>

            </form>
        <?php } ?>
    <?php } ?>
<?php } ?>
<?php include("_footer.phtml") ?>
